********************************************************************************
** 	TITLE:		us2004_anes_contact_networks                                  ** 	
**  AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       anes2004TS                                                    **
**  DATE:	    October 2022 						                          **	
**	VERSION:	Stata 16					                                  **	
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close       			  			              
log using "us2004_anes_contact_networks", replace

* Open the dataset

use "anes2004TS.dta", clear


**************************
** STATE-LEVEL OUTCOMES **
**************************

* Note: 1 = Democrat, 2 = Republican

gen statewinner = .

replace statewinner = 2 if V041202 == 1
replace statewinner = 2 if V041202 == 2
replace statewinner = 2 if V041202 == 4
replace statewinner = 2 if V041202 == 5
replace statewinner = 1 if V041202 == 6
replace statewinner = 2 if V041202 == 8
replace statewinner = 1 if V041202 == 9
replace statewinner = 1 if V041202 == 10
replace statewinner = 1 if V041202 == 11
replace statewinner = 2 if V041202 == 12
replace statewinner = 2 if V041202 == 13
replace statewinner = 1 if V041202 == 15
replace statewinner = 2 if V041202 == 16
replace statewinner = 1 if V041202 == 17
replace statewinner = 2 if V041202 == 18
replace statewinner = 2 if V041202 == 19
replace statewinner = 2 if V041202 == 20
replace statewinner = 2 if V041202 == 21
replace statewinner = 2 if V041202 == 22
replace statewinner = 1 if V041202 == 23
replace statewinner = 1 if V041202 == 24
replace statewinner = 1 if V041202 == 25
replace statewinner = 1 if V041202 == 26
replace statewinner = 1 if V041202 == 27
replace statewinner = 2 if V041202 == 28
replace statewinner = 2 if V041202 == 29
replace statewinner = 2 if V041202 == 30
replace statewinner = 2 if V041202 == 31
replace statewinner = 2 if V041202 == 32
replace statewinner = 1 if V041202 == 33
replace statewinner = 1 if V041202 == 34
replace statewinner = 2 if V041202 == 35
replace statewinner = 1 if V041202 == 36
replace statewinner = 2 if V041202 == 37
replace statewinner = 2 if V041202 == 38
replace statewinner = 2 if V041202 == 39
replace statewinner = 2 if V041202 == 40
replace statewinner = 1 if V041202 == 41
replace statewinner = 1 if V041202 == 42
replace statewinner = 1 if V041202 == 44
replace statewinner = 2 if V041202 == 45
replace statewinner = 2 if V041202 == 46
replace statewinner = 2 if V041202 == 47
replace statewinner = 2 if V041202 == 48
replace statewinner = 2 if V041202 == 49
replace statewinner = 1 if V041202 == 50
replace statewinner = 2 if V041202 == 51
replace statewinner = 1 if V041202 == 53
replace statewinner = 2 if V041202 == 54
replace statewinner = 1 if V041202 == 55
replace statewinner = 2 if V041202 == 56


*********************
** COMPETITIVENESS **
*********************

* Margin of victory by state (absolute difference between Democratic and Republican candidates)

gen margin = .

replace margin = 25.61629 if V041202 == 1
replace margin = 25.54847 if V041202 == 2
replace margin = 10.4726 if V041202 == 4
replace margin = 9.758327 if V041202 == 5
replace margin = 9.947861 if V041202 == 6
replace margin = 4.673252 if V041202 == 8
replace margin = 10.36643 if V041202 == 9
replace margin = 7.594017 if V041202 == 10
replace margin = 79.8441 if V041202 == 11
replace margin = 5.006405 if V041202 == 12
replace margin = 16.59981 if V041202 == 13
replace margin = 8.744957 if V041202 == 15
replace margin = 38.12603 if V041202 == 16
replace margin = 10.34454 if V041202 == 17
replace margin = 20.68179 if V041202 == 18
replace margin = .6675262 if V041202 == 19
replace margin = 25.38089 if V041202 == 20
replace margin = 19.86244 if V041202 == 21
replace margin = 14.50616 if V041202 == 22
replace margin = 8.996399 if V041202 == 23
replace margin = 12.99325 if V041202 == 24
replace margin = 25.02826 if V041202 == 25
replace margin = 3.418648 if V041202 == 26
replace margin = 3.476151 if V041202 == 27
replace margin = 18.85326 if V041202 == 28
replace margin = 7.195747 if V041202 == 29
replace margin = 20.50311 if V041202 == 30
replace margin = 33.21648 if V041202 == 31
replace margin = 2.591652 if V041202 == 32
replace margin = 1.367268 if V041202 == 33
replace margin = 6.684597 if V041202 == 34
replace margin = .7917442 if V041202 == 35
replace margin = 18.44405 if V041202 == 36
replace margin = 12.43405 if V041202 == 37
replace margin = 27.36252 if V041202 == 38
replace margin = 2.107342 if V041202 == 39
replace margin = 31.1408 if V041202 == 40
replace margin = 4.155746 if V041202 == 41
replace margin = 2.500141 if V041202 == 42
replace margin = 20.75199 if V041202 == 44
replace margin = 17.08011 if V041202 == 45
replace margin = 21.46749 if V041202 == 46
replace margin = 14.2738 if V041202 == 47
replace margin = 22.86156 if V041202 == 48
replace margin = 45.54031 if V041202 == 49
replace margin = 20.13615 if V041202 == 50
replace margin = 8.206039 if V041202 == 51
replace margin = 7.180866 if V041202 == 53
replace margin = 12.86557 if V041202 == 54
replace margin = .3798485 if V041202 == 55
replace margin = 39.71648 if V041202 == 56


***************************
** INDEPENDENT VARIABLES **
***************************

* Time

gen sdate = "2004" + V042008

gen surveydate = date(sdate,"YMD")

format %tdMon_DD,_CCYY surveydate

gen edate = 20041102

gen electiondate=date(string(edate,"%8.0f"),"YMD")

format %tdMon_DD,_CCYY electiondate

gen time = electiondate - surveydate

* Gender

gen male = V041109a

recode male (2=0)

* Age

gen age = V043250

recode age (0=.)

replace age = . if age < 18

* Education

gen education = V043254

recode education (0=.)

* Interest for election

gen interest = V043001

recode interest (9=.)

replace interest = 6 - interest

replace interest = 2 if interest == 3
replace interest = 3 if interest == 5

* Caring about outcome (national)

gen care_whole = V043092

recode care_whole (3=0)
recode care_whole (8=.)

* Closeness of election (national)

gen closeness_whole = V043094

recode closeness_whole (88=.)
recode closeness_whole (99=.)

recode closeness_whole (11=0)
recode closeness_whole (12=0)
recode closeness_whole (14=0)
recode closeness_whole (25=1)
recode closeness_whole (26=1)
recode closeness_whole (29=1)

* Closeness of election (state)

gen closeness_state = V043096

recode closeness_state (88=.)
recode closeness_state (89=.)
recode closeness_state (99=.)

recode closeness_state (11=0)
recode closeness_state (12=0)
recode closeness_state (14=0)
recode closeness_state (25=1)
recode closeness_state (26=1)
recode closeness_state (27=1)
recode closeness_state (29=1)

* Party identification scale (national)

gen pidscale = V043116

recode pidscale (7=.) // Other minor party (only one respondent) recoded as missing data
recode pidscale (8=3) // Apolitical recoded as Independent
recode pidscale (9=.) // NA recoded as missing data

replace pidscale = pidscale + 1

gen pidscale_whole = pidscale

label define pidscale 1 "Strong loser PID" 2 "Weak loser PID" 3 "Leaner loser PID" 4 "No PID" 5 "Leaner winner PID" 6 "Weak winner PID" 7 "Strong winner PID"
label values pidscale_whole pidscale

* Party identification scale (state)

gen pidscale_state = pidscale if statewinner == 2
replace pidscale_state = 8 - pidscale if statewinner == 1

label values pidscale_state pidscale

* News attentiveness

gen news = 6 - V045006

* Dennis Hastert - Correct answer (Speaker of the House of Representatives) = 1, Incorrect answer = 0

gen hastert = V045162

recode hastert (5=0)
recode hastert (8=0)

* Dick Cheney - Correct answer (U.S. vice-president) = 1, Incorrect answer = 0

gen cheney = V045163

recode cheney (5=0)
recode cheney (8=0)

* Tony Blair - Correct answer (British Prime Minister) = 1, Incorrect answer = 0

gen blair = V045164

recode blair (5=0)
recode blair (8=0)

* William Rehnquist - Correct answer (Supreme Court Chief Justice) = 1, Incorrect answer = 0

gen rehnquist = V045165

recode rehnquist (5=0)
recode rehnquist (8=0)

* Party with the most members in the U.S. House before election - Correct answer (Republican) = 1, Incorrect answer = 0

gen house = V045089

recode house (1=0)
recode house (5=1)
recode house (8=0)

* Party with the most members in the U.S. Senate before election - Correct answer (Republican) = 1, Incorrect answer = 0

gen senate = V045090

recode senate (1=0)
recode senate (5=1)
recode senate (8=0)

* Political knowledge index

gen knowledge = hastert + cheney + blair + rehnquist + house + senate

* Political discussion

gen discussion = V045153a

recode discussion (8=.)
recode discussion (9=.)

replace discussion = 0 if V045153 == 5

gen discussion_3pts = 0 if discussion < 2 & discussion!=.
replace discussion_3pts = 1 if discussion >= 2 & discussion < 4 & discussion!=.
replace discussion_3pts = 2 if discussion >= 4 & discussion!=.


*********************************
** NATIONAL-LEVEL EXPECTATIONS **
*********************************

* Forecasts (1 = Democrat, 3 = Republican, 7 = other)

gen forecast_whole = V043093

recode forecast_whole (9=.)

* Identify correct forecasts

gen correct_whole_d = 0 if forecast_whole!=3 & forecast_whole!=.
replace correct_whole_d = 1 if forecast_whole == 3


******************************
** STATE-LEVEL EXPECTATIONS **
******************************

* Forecasts (1 = Democrat, 2 = Republican, 3 = other)

gen forecast_state = V043095

recode forecast_state (3=2)
recode forecast_state (5=3)
recode forecast_state (7=3)

* Identify correct forecasts

gen correct_state_d = 0 if forecast_state!=statewinner & forecast_state!=.
replace correct_state_d = 1 if forecast_state == statewinner & forecast_state!=.


**********
** SAVE **
**********

save "us2004_anes.dta", replace


*******************
** MAIN ANALYSES **
*******************

* Regression analysis and percentage of correct and incorrect forecasts (national)

logistic correct_whole_d discussion pidscale_whole interest news knowledge i.care_whole i.closeness_whole age i.male education time
estimates table, star(.05 .01 .001)

tab correct_whole_d if e(sample) == 1

* Regression analysis and percentage of correct and incorrect forecasts (state)

melogit correct_state_d discussion pidscale_state interest news knowledge i.closeness_state age i.male education time margin || V041202: , or
estimates table, star(.05 .01 .001)

tab correct_state_d if e(sample) == 1


************************
** SUMMARY STATISTICS **
************************

label variable correct_whole_d "Presidency winner (DV)"
label variable correct_state_d "State winner (DV)"
label variable discussion "Discussion"
label variable pidscale_whole "National PID"
label variable pidscale_state "State PID"
label variable interest "Interest"
label variable news "News attentiveness"
label variable knowledge "Political knowledge"
label variable care_whole "Caring about outcome"
label variable closeness_whole "Perceived closeness"
label variable closeness_state "Perceived closeness"
label variable age "Age"
label variable male "Gender (male = 1)"
label variable education "Education"
label variable time "Time of interview"
label variable margin "Margin"

quietly logistic correct_whole_d discussion pidscale_whole interest news knowledge i.care_whole i.closeness_whole age i.male education time

estpost tabstat correct_whole_d discussion pidscale_whole interest news knowledge care_whole closeness_whole age male education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "us2004_anes_summary_national.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.2f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2004 American presidential election (ANES), national level") replace

eststo clear

quietly melogit correct_state_d discussion pidscale_state interest news knowledge i.closeness_state age i.male education time margin || V041202: , or

estpost tabstat correct_state_d discussion pidscale_state interest news knowledge closeness_state age male education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "us2004_anes_summary_state.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.2f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2004 American presidential election (ANES), state level") replace

eststo clear

log close